package com.example.weatherapp1.presentation.view

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.weatherapp1.R
import com.example.weatherapp1.databinding.ActivityMainBinding
import com.example.weatherapp1.presentation.viewmodel.MainViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 1. 初始化数据绑定（替换setContentView）
        binding = DataBindingUtil.setContentView(
            this,
            R.layout.activity_main
        )

        // 2. 初始化ViewModel（若用Hilt则无需factory，直接ViewModelProvider(this)）
        viewModel = ViewModelProvider(this)[MainViewModel::class.java]

        // 3. 将ViewModel绑定到布局（关键：让布局识别viewModel变量）
        binding.viewModel = viewModel
        // 4. 设置生命周期所有者（确保LiveData等可观察数据生效）
        binding.lifecycleOwner = this
    }
}